﻿@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Approval Routing Edit";

    bool hasError = false;
    string errorMessage = string.Empty;
    bool hasSuccess = false;
    string successMessage = string.Empty;

    string frm_recommenderId = string.Empty;
    string frm_approverId = string.Empty;

    bool isSecretary = KeppelLand.Controllers.B2EController.IsSecretary(KeppelLand.Helpers.UserIdentityHelper.GetSecretaryName());
    string b2e_section = KeppelLand.Controllers.B2EController.GetSection(KeppelLand.Helpers.UserIdentityHelper.GetSecretaryName());

    KeppelLand.Entities.ReportingStructure reportingStructure = null;
    List<KeppelLand.Entities.Employee> supervisorEmployeeList = null;
    
    if (isSecretary)
    {
        if ((Request.QueryString.Count > 0) && (Request.QueryString["ID"] != null))
        {
            string id = KeppelLand.Helpers.CryptographyHelper.Decrypt(Request.QueryString["ID"]);

            if (IsPost)
            {
                if (Request.Form["actionButton"] == "save_changes")
                {
                    frm_recommenderId = Request.Form["recommenderId"];
                    frm_approverId = Request.Form["approverId"];

                    if (KeppelLand.Controllers.ReportingPeriodController.UpdateReportingStructure(id, frm_recommenderId, frm_approverId))
                    {
                        hasSuccess = true;
                        successMessage = "Approval routing updated.";
                    }
                    
                }
            } // if (IsPost)

            // Retrieve Approval routing based on id
            reportingStructure = KeppelLand.Controllers.ReportingPeriodController.GetReportingStructureByEmployeeMaster(id);
            supervisorEmployeeList = KeppelLand.Controllers.B2EController.GetSupervisorEmployeeList();
        }
    }
}

<div class="container-fluid main-content" role="main">

    @RenderPage("~/Shared/Help_Tab.cshtml")

    @if (hasError)
    {
        <div class="alert alert-danger alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            @Html.Raw(errorMessage)
        </div>
    }
    @if (hasSuccess)
    {
        <div class="alert alert-success alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            @successMessage
        </div>
    }

    <h2>
        Approval Routing for @b2e_section
        @if (KeppelLand.Helpers.UserIdentityHelper.IsSimulatingSecretary())
        {
            <text>(simulating @DisplayHelper.LegalName(KeppelLand.Helpers.UserIdentityHelper.GetSecretaryName()))</text>
        }
    </h2>

    @if (reportingStructure != null)
    {
        @*<h2>Employees</h2>*@

        <form method="post" id="approval_form">

            <table class="table table-striped table-bordered" id="pending_action_claim_list">
                <thead>
                    <tr>
                        <th style="white-space:nowrap;" class="col-lg-3">Name</th>
                        <th style="white-space:nowrap;" class="col-lg-3">Recommending</th>
                        <th style="white-space:nowrap;" class="col-lg-3">Approving</th>
                        <th style="white-space:nowrap;" class="col-lg-3">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>@DisplayHelper.LegalNameByEmployeeMaster(reportingStructure.EmployeeMaster)</td>
                        <td>
                            <select name="recommenderId">
                                <option class="supervisor_dropdownlist" value="">(Not Applicable)</option>
                                @foreach (KeppelLand.Entities.Employee emp in supervisorEmployeeList)
                                {
                                    <option @DisplayHelper.AddSelectedAttributeIf(emp.EmployeeMaster, reportingStructure.RecommenderId) value="@emp.EmployeeMaster">@emp.Section - @emp.NameLegal</option>
                                }
                            </select>
                        
                        </td>
                        <td>
                            <select name="approverId">
                                <option class="supervisor_dropdownlist" value="">(Not Applicable)</option>
                                @foreach (KeppelLand.Entities.Employee emp in supervisorEmployeeList)
                                {
                                    <option @DisplayHelper.AddSelectedAttributeIf(emp.EmployeeMaster, reportingStructure.ApproverId) value="@emp.EmployeeMaster">@emp.Section - @emp.NameLegal</option>
                                }
                            </select>
                        </td>
                        <td>
                            <button type="submit" class="btn btn-primary" name="actionButton" value="save_changes">
                                <span class="glyphicon glyphicon-edit" aria-hidden="true"></span>
                            Save Changes</button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>

        <p>&nbsp;</p>
    }
    else
    {
        if (isSecretary)
        {
            <p>No records found.</p>
        }
        else
        {
            <p>This page is accessible only to secretaries.</p>
        }
    }

</div>

@RenderPage("~/Shared/Standard_Modals.cshtml")

<div class="modal fade" id="helpModal" tabindex="-1" role="dialog" aria-labelledby="helpModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="helpModalLabel">Help <small>(Reject Claim)</small></h4>
            </div>
            <div class="modal-body">
                <p>
                    HR users should use this page to reject claim(s).
                </p>

                <div class="panel panel-default">
                    <div class="panel-heading">Instructions</div>
                    <div class="panel-body">
                        <p>
                            This page is for HR users to reject claims that should not be claimable by user.<br />
                        </p>

                        <ul>
                            <li>HR can click on reject button on the corresponding row to reject that claim.</li>
                            <li>If there are too many entries, user can search for the entry based on username and date and then perform the above step.</li>

                            <li>If there are no claims pending submission, page should indicate as such</li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="helpModal_Close" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

@section Scripts
{
    <script type="text/javascript" src="~/Scripts/kendo/2014.3.1119/kendo.ui.core.min.js"></script>
    <script type="text/javascript" src="~/Scripts/moment.js"></script>
    <script type="text/javascript" src="~/Scripts/moment-with-locales.js"></script>
    <script type="text/javascript">
    (function ($) {
        "use strict";
        $(document).ready(function () {

            $("supervisor_dropdownlist").kendoDropDownList();

            // User clicked reject
            $("#actionButton_reject").click(function () {
                $("#approval_form #actionButton").val(this.value);
                $("#rejectionModal").modal('show');
            });
            $("#rejectionModal_OK").click(function () {
                $("#approval_form #rejection_reason").val(
                    $("#rejectionModal #rejection_reason_entry").val());
                $("#approval_form").submit();
            });
        });
    }(jQuery));

</script>
}